/******************************************************************************
 *                                                                            *
 *                    OS configuration header file                            *
 *                                                                            *
 *----------------------------------------------------------------------------*
 *              --- Included files and compilation variables ---              *
 *----------------------------------------------------------------------------*
 *****************************************************************************/

// Insert here the differents header files corresponding to your application

#include <hidef.h>      /* common defines and macros */

#include "OS.h"
#include "OS_Task.h"


/*----------------------------------------------------------------------------*
 *                     --- Local constants & typedefs ---                     *
 *----------------------------------------------------------------------------*/

extern void taskScan(void);
extern void taskRefresh(void);
extern void taskChecker(void);



/*
    Declare Tasks here
    DECLARE_TASK(task_name, period_in_ms)   if period_in_ms is 0, task is inactive at startup
    Task must be ordered like OS_TaskID declared in OS_Cfg.h
    task prototype is void-void, see pTaskHandler typedef
*/
    BEGIN_TASKS
// Insert here the differents tasks
//      DECLARE_TASK(tash handler function, Task periodicity 0 if suspended at startup)
    DECLARE_TASK(taskScan,    0)    
    DECLARE_TASK(taskRefresh, 0)    
    DECLARE_TASK(taskChecker, 200)    

    END_TASKS



/*----------------------------------------------------------------------------*
 *                             --- Variables ---                              *
 *----------------------------------------------------------------------------*/

signed short OS_AllInterruptsCounter=0;


/*----------------------------------------------------------------------------*
 *                    --- Local functions declaration ---                     *
 *----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*
 *                          --- Local functions ---                           *
 *----------------------------------------------------------------------------*/

#pragma CODE_SEG NON_BANKED

/******************************************************************************
 * Function  : OS_InitTimer
 *
 * Role      : Timer initialisation (hardware)
 *
 *-----------------------------------------------------------------------------
 * Entry     : <<entry parameters>>
 *
 * Exit      : <<exit parameters>>
 *
 *-----------------------------------------------------------------------------
 * globals   : r/w Name Of Variable    Range       Description
 *             --- ------------------  ---------   ----------------------------
 *
 *
 ******************************************************************************/

void OS_InitTimer(void)
{
// Insert here the code to start the timer for the OS tick
#ifdef _WIN32
    timeSetEvent(OS_SYS_TICK/1000,0, OS_SysTick,0,TIME_PERIODIC);
#else
	// Enable Timer 4 for 1ms click
    TIOS        = 0x10;
    TIE_C4I     = 1;
    TSCR1_TEN   = 1;
    TSCR1_TSWAI = 1;
    TSCR1_TSFRZ = 1;

	// Run timer
	TC4 = OS_TIMER_PERIOD;
#endif
}

/******************************************************************************
 * Function  : OS_InitInterrupt
 *
 * Role      : Initialisation of all interrupt
 *
 *-----------------------------------------------------------------------------
 * Entry     : <<entry parameters>>
 *
 * Exit      : <<exit parameters>>
 *
 *-----------------------------------------------------------------------------
 * globals   : r/w Name Of Variable    Range       Description
 *             --- ------------------  ---------   ----------------------------
 *
 *
 ******************************************************************************/

void OS_InitInterrupt(void)
{
// Insert here the code to start the interrupt controller
#ifndef _WIN32
	// Elevate HPRIO for TC4
	HPRIO  = 0xE6;
#endif    
	OS_EnableAllInterrupts();
}

/*----------------------------------------------------------------------------*
 *                          --- Public functions ---                          *
 *----------------------------------------------------------------------------*/

/******************************************************************************
 * Function  : OS_StartupHook
 *
 * Role      : Startup function
 *
 *-----------------------------------------------------------------------------
 * Entry     : <<entry parameters>>
 *
 * Exit      : <<exit parameters>>
 *
 *-----------------------------------------------------------------------------
 * globals   : r/w Name Of Variable    Range       Description
 *             --- ------------------  ---------   ----------------------------
 *
 *
 ******************************************************************************/
  
void OS_StartupHook(void)
{
// Insert here all the user functions that must be called in the OS Startup function
#ifndef _WIN32

//  _DISABLE_COP();
  _ENABLE_COP(0x46);

#ifdef PLL_INUSE
	SYNR   = PLL_MUL-1;	// 2 * (0+1)         = 1xFxtal MHz
	REFDV  = PLL_DIV-1;	// 	 / (0+1)

	/* Select Auto mode for PLL	 */
	PLLCTL |= 0x20;

	/* Wait that PLL is locked	 */
	while( (CRGFLG & 0x08) != 0x08 );
						\
	/* Select PLL			 */
	CLKSEL |= 0x80;
#endif
   	
#endif
    OS_InitInterrupt();
    OS_InitTimer();

// Insert here all the user functions that must be called in the OS Startup function
}

// The three following functions aren't used. However, the code has been kept in comments.

/******************************************************************************
 * Function  : OS_EnableInterrupt
 *
 * Role      : Enable one interrupt
 *
 *-----------------------------------------------------------------------------
 * Entry     : <<entry parameters>>
 *
 * Exit      : <<exit parameters>>
 *
 *-----------------------------------------------------------------------------
 * globals   : r/w Name Of Variable    Range       Description
 *             --- ------------------  ---------   ----------------------------
 *
 *
 ******************************************************************************/
/*
PUBLIC void
OS_EnableInterrupt(OS_InterruptID InterrruptID)
{
#if defined(_uC_16CDC1620_)||defined (_uC_16CDC1607F_E1_)
	unsigned char ucMask;
	
	if (InterrruptID & 1)	 // odd or even ?
	{
		ucMask = 0xF0;
	}
	else
	{
		ucMask = 0x0F;
	}
	// set interrupt level for this interrupt
	InterrruptID >>= 1;	// Divide by 2, make sure this is optimised
	*(IntRegMap[InterrruptID].IRPRx) = *(IntRegMap[InterrruptID].IRPRx) |
		(IntRegMap[InterrruptID].IRPRxPrio & ucMask);
#endif
}
*/
/******************************************************************************
 * Function  : OS_DisableInterrupt
 *
 * Role      : Disable one interrupt
 *
 *-----------------------------------------------------------------------------
 * Entry     : <<entry parameters>>
 *
 * Exit      : <<exit parameters>>
 *
 *-----------------------------------------------------------------------------
 * globals   : r/w Name Of VaVariable    Range       Description
 *             --- ------------------  ---------   ----------------------------
 *
 *
 ******************************************************************************/
/*
PUBLIC void
OS_DisableInterrupt(OS_InterruptID InterrruptID)
{
#if defined(_uC_16CDC1620_)||defined (_uC_16CDC1607F_E1_)
	unsigned char ucMask;
	
	if (InterrruptID & 1)	 // odd or even ?
	{
		ucMask = 0x0F;
	}
	else
	{
		ucMask = 0xF0;
	}
	// set interrupt level for this interrupt
	InterrruptID >>= 1;	// Divide by 2, make sure this is optimised
	*(IntRegMap[InterrruptID].IRPRx) = *(IntRegMap[InterrruptID].IRPRx) & ucMask;
#endif
}
*/

/******************************************************************************
 * Function  : AppHalt
 *
 * Role      : Halt the application.
 *
 *-----------------------------------------------------------------------------
 * Entry     : None
 *
 * Exit      : None
 *
 *-----------------------------------------------------------------------------
 * Globals   : r/w Name Of Variable  Range              Description
 *
 ******************************************************************************/

/*PUBLIC void AppHalt(void)
{
//	ShutdownOS(0);
}*/

#pragma CODE_SEG DEFAULT

/******************************************************************************
 *                               End of file                                  *
 ******************************************************************************/


